using System;

namespace UnitTesting.Fixturing
{
	class TestObjectCreator<T>
	{
		readonly IValueProvider _valueProvider;

		readonly IFreezable _freezable;

		protected TestObjectCreator()
		{
		}

		internal TestObjectCreator(IValueProvider valueProvider, IFreezable freezable)
		{
			_valueProvider = Ensure.IsNotNull(() => valueProvider);
			_freezable = freezable;
		}

		public virtual T Create()
		{
			return (T) CreateImp(typeof (T));
		}

		object CreateImp(Type type)
		{
			return _valueProvider.Create(type);
		}

		public T[] CreateMany()
		{
			return (T[]) CreateImp(typeof (T[]));
		}

		public T[] CreateMany(int count)
		{
			var result = new T[count];
			for (int i = 0; i < result.Length; i++)
			{
				result[i] = Create();
			}
			return result;
		}

		internal virtual T Freeze()
		{
			var result = Create();
			_freezable.Freeze(result);
			return result;
		}
	}
}